package com.iszhouhua.blog.controller.vue;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iszhouhua.blog.common.util.Result;
import com.iszhouhua.blog.model.Category;
import com.iszhouhua.blog.model.Video;
import com.iszhouhua.blog.service.CategoryService;
import com.iszhouhua.blog.service.VedioService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 视频管理
 *
 * @author xiaojin
 * @since 2020-10-10
 */
@RestController
@CrossOrigin
@RequestMapping("video")
public class VedioController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private VedioService vedioService;

  /**
   * 分页查询
   * @param page
   * @return
   */
    @GetMapping("list")
    public Result list(Page<Video> page,String category_id) {

      QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
      if (StringUtils.isNotBlank(category_id)){
        queryWrapper.eq("category_id",category_id);
      }
      queryWrapper.orderByDesc("update_time");
      IPage<Video> videoIPage = vedioService.page(page,queryWrapper);

      for (Video video1 : videoIPage.getRecords()){
        Category category = categoryService.getById(video1.getCategoryId());
        video1.setCategory(category);
      }
      return new Result(20000,"查询成功", videoIPage);
    }


  /**
   * 根据id获取视频
   * @param id
   * @return
   */
    @GetMapping
    public Result info(Long id) {
      Video video = vedioService.getById(id);
      Category category = categoryService.getById(video.getCategoryId());
      video.setCategory(category);
      return new Result(20000,"获取成功", video);
    }


}
